/**
 * 
 */
package com.olclass.web.support;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.olclass.web.support.FileUploadProgressListener;

public class CommonsMultipartResolverExt extends CommonsMultipartResolver  {
	private HttpServletRequest request;
	
	protected List<String> excludePaths = new ArrayList<String>();
	
	public void setExcludePaths(List<String> excludePaths) {
		this.excludePaths = excludePaths;
	}

	@Override  
    public boolean isMultipart(HttpServletRequest request) {
          
        String uri = request.getRequestURI();  
        
        for(String excludePath:excludePaths){
        	 if (uri.indexOf(excludePath) > 0) {   
                 return false;  
             }  
        }
       
        return super.isMultipart(request);  
    }  
	
	@Override
	protected  FileUpload newFileUpload(FileItemFactory fileItemFactory)
	{
	    ServletFileUpload upload = new ServletFileUpload(fileItemFactory); 
    	upload.setSizeMax(-1); 
	    if(request!=null)
	    {
	        //System.out.println("注入监听");
	    	HttpSession session = request.getSession();
	    	String workId = request.getParameter("workId") == null ? FileUploadProgressListener.UploadStatus:request.getParameter("workId").toString();
	    	FileUploadProgressListener uploadProgressListener = new FileUploadProgressListener(session,workId);
	        upload.setProgressListener(uploadProgressListener);
	     }
	    return upload;
	}
	@Override
	public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
	       this.request=request;//获取到request,要用到session
	       return super.resolveMultipart(request);
	 }
	
	@Override
	public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
		HttpSession session = request.getSession(); 
		String encoding = determineEncoding(request);
		FileUpload fileUpload = prepareFileUpload(encoding);
		String workId = request.getParameter("workId") == null ? FileUploadProgressListener.UploadStatus:request.getParameter("workId").toString();
		FileUploadProgressListener progressListener = new FileUploadProgressListener(session,workId);
		fileUpload.setProgressListener(progressListener);  
		try {
			List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
			return parseFileItems(fileItems, encoding);
		} catch (FileUploadBase.SizeLimitExceededException ex) {
			throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
		} catch (FileUploadException ex) {
			throw new MultipartException("Could not parse multipart servlet request", ex);
		}
	}
}
